package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.AccountEntity;
import io.renren.modules.sys.entity.InoutitemEntity;
import io.renren.modules.sys.service.InoutitemService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 收支项目
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-22 14:29:36
 */
@RestController
@RequestMapping("sys/inoutitem")
public class InoutitemController {
    @Autowired
    private InoutitemService inoutitemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:inoutitem:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = inoutitemService.queryPage(params);

        return R.ok().put("page", page);
    }


    @RequestMapping("/findBySelectNew")
    public List<InoutitemEntity> findBySelectNew(@RequestParam Map<String, Object> params){

        String type=params.get("type").toString();
        if("in".equals(type)){
            type="收入";
        }else{
            type="支出";
        }

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<InoutitemEntity> page = inoutitemService.selectPage(
                new Query<InoutitemEntity>(params).getPage(),
                new EntityWrapper<InoutitemEntity>().like("type", type).orderBy("id")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<InoutitemEntity> dataList = (List<InoutitemEntity>) pageUtils.getList();

        JSONArray dataArray = new JSONArray();
        for (int i = 0; i < dataList.size(); i++) {
            JSONObject item = new JSONObject();
            InoutitemEntity inoutitem = (InoutitemEntity) dataList.get(i);
            item.put("Id", inoutitem.getId());
            item.put("InOutItemName", inoutitem.getName()); //客户名称
            dataArray.add(item);
        }

        return dataArray;

    }


    @RequestMapping("/findBySelect")
    public R findBySelect(@RequestParam Map<String, Object> params){

        String type=params.get("type").toString();
        if("in".equals(type)){
            type="收入";
        }else{
            type="支出";
        }

        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<InoutitemEntity> page = inoutitemService.selectPage(
                new Query<InoutitemEntity>(params).getPage(),
                new EntityWrapper<InoutitemEntity>().like("type", type).orderBy("id")
        );

        PageUtils pageUtils = new PageUtils(page);
        List<InoutitemEntity> nativeName1 = (List<InoutitemEntity>) pageUtils.getList();
        return R.ok().put("rows", nativeName1).put("total",nativeName1.size());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:inoutitem:info")
    public R info(@PathVariable("id") Long id){
        InoutitemEntity inoutitem = inoutitemService.selectById(id);

        return R.ok().put("inoutitem", inoutitem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:inoutitem:save")
    public R save(@RequestBody InoutitemEntity inoutitem){
        inoutitemService.insert(inoutitem);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:inoutitem:update")
    public R update(@RequestBody InoutitemEntity inoutitem){
        ValidatorUtils.validateEntity(inoutitem);
        inoutitemService.updateAllColumnById(inoutitem);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:inoutitem:delete")
    public R delete(@RequestBody Long[] ids){
        inoutitemService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
